package net.jsoft.platform.security.web.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.jsoft.platform.core.web.BaseController;
import net.jsoft.platform.oauth2.util.ThirdPartyConstant;
import net.jsoft.platform.security.entity.parms.SecurityParms;
import net.jsoft.platform.security.entity.user.SysUser;
import net.jsoft.platform.security.repository.SecurityParmsRepository;
import net.jsoft.platform.security.repository.UserRepository;

@Controller
public class IndexController extends BaseController
{

    @Autowired
    ThirdPartyConstant tpc;

    @Autowired
    SecurityParmsRepository repository;

    @Autowired
    UserRepository userRepository;

    @RequestMapping(value =
    { "/login", "/" })
    public String loginPage(Model model)
    {
        String page = null;
        String github = tpc.getGithubUserAuthorizationUri() + "?client_id=" + tpc.getGithubClientId();
        String weibo = tpc.getWeiboUserAuthorizationUri() + "?response_type=code&client_id=" + tpc.getWeiboClientId() + "&redirect_uri=" + tpc.getWeiboRedirectUri();
        model.addAttribute("github", github);
        model.addAttribute("weibo", weibo);

        if (this.getUser() == null)
        {
            page = "/platform/login/login";
        }
        else
        {
            page = "redirect:/index";
        }
        return page;
    }

    @RequestMapping("/index")
    public String login(Model model)
    {
        SecurityParms globalIndexParams = repository.selectParms("GLOBALINDEX");
        String globalIndex = "/platform/index/index";
        if (globalIndexParams != null)
        {
            globalIndex = globalIndexParams.getValue();
        }
        String index = this.getUser().getIndexPage();
        if (StringUtils.isEmpty(index))
        {
            index = globalIndex;
        }
        return index;
    }

    @RequestMapping(value = "/logout")
    public String logoutPage(HttpServletRequest request, HttpServletResponse response)
    {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null)
        {
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/login";//You can redirect wherever you want, but generally it's a good practice to show login screen again.  
    }

    @RequestMapping("/indexUI")
    @ResponseBody
    public String indexUI()
    {
        SecurityParms globalIndexParams = repository.selectParms("GLOBALINDEX");
        String globalIndex = "/platform/index/index";
        if (globalIndexParams != null)
        {
            globalIndex = globalIndexParams.getValue();
        }
        String index = this.getUser().getIndexPage();
        if (StringUtils.isEmpty(index))
        {
            index = globalIndex;
        }
        return index;
    }

    @RequestMapping("/indexUIPage")
    public String indexUIPage(Model model)
    {
        model.addAttribute("userUI", indexUI());
        return "/platform/index/changeIndex";
    }

    @RequestMapping("/saveIndexUI")
    @ResponseBody
    public void saveIndexUI(String path)
    {

        SysUser user = userRepository.getOne(this.getUser().getId());
        user.setIndexPage(path);
        userRepository.save(user);
        this.saveIndexUIInSession(path);

    }

}
